package cn.suhaoblog.generate;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;

import java.util.Collections;
import java.util.HashMap;

// 注意修改以下全局变量
public class CodeGenerateTools {

    // 要生成模块的路径
    public static final String GLOBAL_MODULE_NAME = "/services/service-faq-8001";
    // 模块名, 包全路径最后一个单词
    public static final String PARENT_NAME = "faq";
    // java主路径
    public static final String JAVA_DIR = "/src/main/java";
    // resources路径
    public static final String RESOURCES_DIR = "/src/main/resources";
    // 生成mapper.xml路径, 前面需要拼接RESOURCES_DIR
    public static final String MAPPER_LOCATION = "/mybatis/mappers";
    public static final String PARENT = "cn.suhaoblog";

    // 待生成表集合
    public static final String[] TABLES = new String[]{"interview_faq"};

    public static void main(String[] args) {

        AutoGenerator generator = new AutoGenerator();

        // ===================  全局配置  ===================
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig
                .setOutputDir(System.getProperty("user.dir") + GLOBAL_MODULE_NAME + JAVA_DIR)
                .setAuthor("su-hao")
                .setOpen(false) // 生成后打开文件
                .setFileOverride(false) // 覆盖以前生成的文件
                .setIdType(IdType.ID_WORKER_STR).setDateType(DateType.ONLY_DATE)
                .setSwagger2(true) // 开启swagger
                .setBaseResultMap(true)
                // 别名部分配置
                .setServiceName("%sService");

        generator.setGlobalConfig(globalConfig);

        // ======================  数据源配置  ======================
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig
                .setDbType(DbType.MYSQL)
                .setUrl("jdbc:mysql://localhost:3306/all_in_one?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false")
                .setUsername("root")
                .setPassword("1501719577")
                .setDriverName("com.mysql.cj.jdbc.Driver");
        generator.setDataSource(dataSourceConfig);

        // ===================  包配置  ===================
        PackageConfig packageConfig = new PackageConfig();
        packageConfig
                // cn.suhaoblog.[PARENT_NAME]最后一个为moduleName, 前面为parent
                .setParent(PARENT)
                .setModuleName(PARENT_NAME)
                .setServiceImpl("impl")
                .setController("controller")
                .setEntity("entity")
                .setXml("mapper")
                .setMapper("dao");

        // 配置输出路径, 这个版本generator代码有问题, 必须配置所有的要生成文件路径
        // 未配置路径不会生成, 高版本没这个问题, 可以直接通过pathInfo覆盖默认路径
        String root = System.getProperty("user.dir");
        HashMap<String, String> pathInfo = getStringStringHashMap(root, packageConfig);
        packageConfig.setPathInfo(pathInfo);

        generator.setPackageInfo(packageConfig);

        // ===================  策略配置  ===================
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig
                // 全局名称转换策略,表和字段未指定都按照这个策略执行
                .setNaming(NamingStrategy.underline_to_camel)

                // Controller设置
                .setSuperControllerClass("cn.suhaoblog.core.base.BaseController")
                .setRestControllerStyle(true) // 开启@RestController

                // Entity设置
                .setSuperEntityClass("cn.suhaoblog.core.base.BaseEntity")
                .setEntityLombokModel(true) // 开启lombok

                // 待生成表
                .setInclude(TABLES);

        generator.setStrategy(strategyConfig);

        // ===================  模板配置  ===================
        generator.setTemplateEngine(new FreemarkerTemplateEngine());
        TemplateConfig templateConfig = new TemplateConfig();
        // 自定义controller模板
        templateConfig.setController("ftl/controller.java");
        generator.setTemplate(templateConfig);

        generator.execute();
    }

    private static HashMap<String, String> getStringStringHashMap(String root, PackageConfig packageConfig) {
        HashMap<String, String> pathInfo = new HashMap<>(6);
        pathInfo.put(ConstVal.ENTITY_PATH, root + GLOBAL_MODULE_NAME + JAVA_DIR + "/" + PARENT.replace('.', '/') + "/" + PARENT_NAME + "/" + packageConfig.getEntity());
        pathInfo.put(ConstVal.SERVICE_PATH, root + GLOBAL_MODULE_NAME + JAVA_DIR + "/" + PARENT.replace('.', '/') + "/" + PARENT_NAME + "/" + packageConfig.getService());
        pathInfo.put(ConstVal.SERVICE_IMPL_PATH, root + GLOBAL_MODULE_NAME + JAVA_DIR + "/" + PARENT.replace('.', '/') + "/" + PARENT_NAME + "/" + packageConfig.getServiceImpl());
        pathInfo.put(ConstVal.MAPPER_PATH, root + GLOBAL_MODULE_NAME + JAVA_DIR + "/" + PARENT.replace('.', '/') + "/" + PARENT_NAME + "/" + packageConfig.getMapper());
        pathInfo.put(ConstVal.CONTROLLER_PATH, root + GLOBAL_MODULE_NAME + JAVA_DIR + "/" + PARENT.replace('.', '/') + "/" + PARENT_NAME + "/" + packageConfig.getController());
        pathInfo.put(ConstVal.XML_PATH, root + GLOBAL_MODULE_NAME + "/" + RESOURCES_DIR + MAPPER_LOCATION);
        return pathInfo;
    }

}
